package com.sykj.bsc.rwl.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sykj.bsc.rwl.po.SysUser;
import com.sykj.bsc.rwl.po.extend.SysMenuExtend;
import com.sykj.bsc.rwl.service.SysMenuService;

@Controller
@RequestMapping("menu")
public class SysMenuController {
	
	@Autowired
	private SysMenuService sysMenuService;
	
	/**
	 * @Title: selectAllSysMenu   
	 * @Description: 菜单配置   
	 * @param: @param req
	 * @param: @param model
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("selectAllSysMenu")
	public String selectAllSysMenu(HttpServletRequest req,Model model) throws Exception {
		SysUser sysUser=getSysUser(req);
		int user_id=sysUser.getUser_id();
		String data=sysMenuService.selectAllMenu(user_id);
		model.addAttribute("msg", data);
		return "backstage/rwl/listAll";
	}
	
	public SysUser getSysUser(HttpServletRequest req) {
		HttpSession session=req.getSession();
		SysUser sysUser=(SysUser) session.getAttribute("user");
		return sysUser;
	}
	
	@RequestMapping("selectPermission")
	public String selectPermission(HttpServletRequest req,Model model)throws Exception {
		SysUser sysUser=getSysUser(req);
		int user_id=sysUser.getUser_id();
		String data=sysMenuService.selectPermisson(user_id);
		model.addAttribute("data", data);
		return "backstage/rwl/permisson";
	}
	
	@RequestMapping("selectPermission2")
	public String selectPermission2(@RequestParam("role_id") int role_id,Model model)throws Exception {
		if(role_id!=0) {
			model.addAttribute("role_id", role_id);
		}
		String data=sysMenuService.selectPermission2(role_id);
		model.addAttribute("data", data);
		return "backstage/rwl/permisson";
	}
	
	/**
	 * @throws Exception 
	 * @Title: savePermission   
	 * @Description: 保存或修改权限  
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("savePermission")
	public void savePermission(SysMenuExtend sysMenuExtend,HttpServletRequest req,HttpServletResponse resp) throws Exception {
		SysUser sysUser=getSysUser(req);
		sysMenuExtend.setCreate_user_id(sysUser.getUser_id()+"");
		sysMenuService.savePermission(sysMenuExtend);
		SysUserController.getWirte(resp, null);
	}
}
